<?php

namespace app\common\model\Project;
// 引入模型
use think\Model;

// 预约项目模型
class Order extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'project_order';

    // 自动插入的时间戳字段
    protected $autoWriteTimestamp = 'int';
    
    //  创建时间字段
    protected $createTime = 'createtime';
    protected $updateTime = false;

    // 追加字段
    protected $append  = [
        'status_text', // 订单状态
        'createtime_text', 
    ];

    public function getStatusTextAttr($value,$data)
    {
        $status = empty($data['status']) ? '' : $data['status'];
        switch ($status){
            case 0:
                return "未开始";
                break;
            case 1:
                return "已开始";
                break;
            case 2:
                return "已结束";
                break;
            default:
                return '未知状态';
        }
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime = isset($data['createtime']) ? $data['createtime'] : '';

        if(empty($createtime))
        {
            return '';
        }
        
        return date('Y-m-d H:i', $createtime);
    }

    public function project()
    {
        return  $this->belongsTo('app\common\model\Project\Project', 'pid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function business()
    {
        return  $this->belongsTo('app\common\model\Business\Business', 'busid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
